$().ready(function() {
	ifState()
	imageCreate()
	validateRule()
	radioChange()


});

// 页面判断
function ifState() {
	let state = $('#authority').val()
	if(state === '管理员') {
		$('.Administrator').show()
		$('.teacher').hide()
	}else {
		$('.teacher').show()
		$('.Administrator').hide()
	}
}

$.validator.setDefaults({
	submitHandler : function() {
		save();
	}
});

// 图片处理
function imageCreate() {
	$('#imgFil').click(function (){
		var imageUrl = $(this).attr('src');
		$('.overlay').toggleClass('active');
		$('.overlay img').attr('src', imageUrl);
	})
	$('.overlay').click(function() {
		$(this).removeClass('active');
	});

}


// 页面判断
function radioChange() {
	let radios = document.getElementsByName('publicity');
	let radiosAll = document.getElementsByName('thumbnail');
	for (let i = 0; i < radios.length; i++) {
		radios[i].addEventListener('change', function() {
			if(this.value === '1') {
				$('#selectChange').show()
			}else {
				$('#selectChange').hide()
			}
		});
	}
	for (let i = 0; i < radiosAll.length; i++) {
		radiosAll[i].addEventListener('change', function() {
			if(this.value === '1') {
				$('#selectChanges').show()
			}else {
				$('#selectChanges').hide()
			}
		});
	}
}

// 上传
function save() {
	let imgs = $('#imgAll').children().length
	if(imgs === 0) {
		layer.msg("请上传资源封面")
		return
	}
	let img = $('#imgFil').attr('src')
	let formData = $('#signupForm').serializeArray();
	let  data = {};
	$.each(formData, function(index, item) {
		// .replace(item.name[0],item.name[0].toUpperCase())
		data[item.name] = item.value;

	});
	// data.newType === '' ?  data['publicity'] = 1 : data['publicity'] = 0
	data['value'] = img

	addFrom(data)

}


// 新增
function addFrom(data) {

	return new Promise(function (resolve, reject) {
		$.ajax({
			url: '/banner/add',
			type: 'POST',
			contentType: 'application/json',
			data: JSON.stringify(data),
			success: function(response) {
				// 请求成功时的处理
				if(response.code !== '0') {
					layer.msg(response.msg);
				}else {
					let index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
					parent.layer.close(index);
					parent.reLoad();
					reject(response)
				}

			},
			error: function(xhr, status, error) {
				// 请求失败时的处理
				layer.msg(error);
				resolve(error)
			}
		});
	})

}

function validateRule() {
	var icon = "<i class='fa fa-times-circle'></i> ";
	$("#signupForm").validate({
		rules : {
			name:{
				required : true
			},
			type : {
				required : true
			},
			// value : {
			// 	required : true
			// },
			// level : {
			// 	required : true
			// }
		}
	})
}

// 上传
 layui.use('upload', function () {
        var upload = layui.upload;
        //执行实例
        upload.render({
            elem: '#btnUpload', //绑定元素
			url: 'http://10.3.0.64:9900/minio/uploadFile',
			// url: 'http://192.168.1.108:9900/minio/uploadFile',
			acceptMime: 'image/*',
            accept: 'images',
			field:'file',
            done: function (r) {
				$('#imgAll').html(`
<img src="${r.data}" id="imgFil"  style="max-width: 50%;height: auto;cursor: pointer;" />`)
            },
            error: function (r) {
                layer.msg(r.msg);
            }
        });
    });